//商家列表 0
//添加商家 1
//商家详情 2
//备付金账户 3
//商家修改  4
//添加备付金 5
//结算账户列表 6
//绑定银行卡 7
//店员列表 8
//新增店员 9
define(function(require, exports, module){
	"require:nomunge,exports:nomunge,module:nomunge";
	//异步请求
var wind_async=require('ddf_wind_async');
	
var base=require('component-base');
var history = require('component-history');
	
require('component-page');

var shopname = "",mobile = "",shopId="";

exports.init=function(m){
	   
    window.$ddf_shop = m;
    history.addHis(this,arguments);
    
    
    
	var htmlStr = "";
		
		htmlStr += '<div id="content-header">';
		htmlStr += '<div id="breadcrumb"> <a href=javascript:load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:void(0)" class="current">商家查询</a></div>';
//		htmlStr += '<h1>商家查询</h1>';
		htmlStr += '</div>    ';

		htmlStr += '<div class="container-fluid">';
//		htmlStr += '<hr/>';

		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
		htmlStr += '<div id="alert-tips0" ></div>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-search"></i></span>';
		htmlStr += '<h5>搜索条件</h5><button type="button" class="label label-success toggle">缩放</button>';
		htmlStr += '</div>        ';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<form class="form-horizontal">';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span6">';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">商家代码：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span8 m-wrap" type="text" id="shopId"/>';
		htmlStr += '</div>                          ';
		htmlStr += '</div>';
		htmlStr += '</div>';
	
		htmlStr += '</div> ';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span6">';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">商家名称：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span8 m-wrap" type="text" id="shopname"/>';
		htmlStr += '</div>     ';
		htmlStr += '</div>';
		htmlStr += '</div>  ';
		htmlStr += '</div>   ';
		htmlStr += '<div class="form-actions">';
		htmlStr += '<button class="btn btn-primary" type="button" onClick="$ddf_shop.searchShop()" >搜索</button>';
		htmlStr += ' <button class="btn btn-info" type="button" onClick="$ddf_shop.resetSearchParams()">重置</button>';
		htmlStr += '</div>         ';
		htmlStr += '</form>';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>用户列表</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered data-table">';
		htmlStr += '<thead>';
		htmlStr += '<tr>';
		htmlStr += '<th>序号</th>';
		htmlStr += '<th>商家代码</th>';
		htmlStr += '<th>商家名称</th>';
		htmlStr += '<th>手机号码</th>';
		htmlStr += '<th>绑定会员数</th>';
		htmlStr += '<th>游客数</th>';
		htmlStr += '<th>创建时间</th>';
		htmlStr += '</tr>';
		htmlStr += '</thead>';
		htmlStr += '<tbody id="shopList">';
		
		htmlStr += '</tbody>';
		htmlStr += '</table>';
		htmlStr += '<div id="footPageDiv0">';

		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '</div>  ';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div>';
    	showDiv("content0");
    	$("#content0").html(htmlStr);
    	getShopList(1,pageSize,shopname,mobile,shopId);
}

/**
 * 获取商家列表
 * @param pageNumber
 * @param shopname
 * @param mobile
 * @param agentId
 */
function getShopList(pageNumber,pageSize,shopname,mobile,shopId){
	
	var params={
			"page.pageNumber":pageNumber,
			"page.pageSize":pageSize,
			"shopName":shopname,
			"shopId":shopId
	        };
	
	base.showLoading();
	
	eval(Wind.compile("async",function(){
		
		var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.shop.queryShopsByAgentId,params));
		
		getShopListCallBack(data);
		
	}))().start();
}

/**
 * 获取商家列表_回调函数
 */
function getShopListCallBack(data){
	
	if(data["code"]==1){
	  
		var htmlStr="";
		
		for(var i=0;i<data["resultList"].length;i++){
			htmlStr += '<tr>';
			htmlStr += '<td>'+getSequence(data["pageNumber"],i+1)+'</td>';
			htmlStr += '<td>'+data["resultList"][i]["shopId"]+'</td>';
			htmlStr += '<td>'+data["resultList"][i]["shopName"]+'</td>';
			htmlStr += '<td>'+data["resultList"][i]["mobile"]+' </td>';
			htmlStr += '<td>'+data["resultList"][i]["bindUserCount"]+' </td>';
			htmlStr += '<td>'+data["resultList"][i]["noBindUserCount"]+' </td>';
			htmlStr += '<td>'+data["resultList"][i]["createTime"]+'</td>';
			htmlStr += '</tr> ';
		}
		
		$("#shopList").html(htmlStr);
		setPage(pageIndex,data["pageNumber"],data["totalCount"],data["totalPage"],"$ddf_shop");
		$('#footPageDiv0').ddf_pagination(data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"],{  
			 
			 changePageCallback:function(pageSize,page){  
				 getShopList(page,pageSize,shopname,mobile,shopId);

			 },
			 changePageSizeCallBack:function(pageSize){
				 getShopList(1,pageSize,shopname,mobile,shopId);
	     	 
			 } 
	     }); 
		
	}else{
		
		 base.tips_error(data["message"]);
	}

	base.cancelLoading();
}


/**
 * 重置参数
 */
function resetSearchParams(){
	
	if(pageIndex==0){
		
		shopname="";
		
		mobile="";
		
		shopId="";
		
		$("#shopname").val("");
		
		$("#shopId").val("");
		
	}
	
}
/**
 * 搜索商家
 */
function searchShop(){
	
     
     shopname=$("#shopname").val();
	
     shopId=$("#shopId").val();
	
     
     $ddf_shop.getShopList(1,pageSize,shopname,mobile,shopId);
	
}




/**
 * 显示DIV
 */
function showDiv(block){
	
	$("#content > div").hide();
	
	if($("#"+block).length<=0){
		
		$("#content").append('<div id="'+block+'"></div>'); 
		
	}else{
		
		$("#"+block).show();
	}
	
	if(block=="content0"){
		shopTypeId="";
	}
	
}

/**
 * 对外提供访问的方法
 */
   exports.getShopList = getShopList; //获取商家列表
   exports.resetSearchParams =resetSearchParams; //重置参数
   exports.searchShop = searchShop; //搜索
   exports.showDiv = showDiv; 
});


